package com.ajiew.phonecallapp.my_ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ajiew.phonecallapp.R;
import com.ajiew.phonecallapp.my_ui.adapter.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyDialActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView rcv_num;// 拨号盘
    private TextView tv_num;// 展示预拨的号码
    private Button btn_dial, btn_reset;// 拨号,重输

    private ArrayList<String> dialNumList = new ArrayList<>();
    private PhoneNumAdapter adapter;
    private Map<String, String> PHONE_NUMBER = new HashMap<>();// 短号与号码对应关系

    private StringBuffer sb = new StringBuffer();// 用于记录拨叫的号码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_dial);
        rcv_num = findViewById(R.id.rcv_num);
        tv_num = findViewById(R.id.tv_num);
        btn_dial = findViewById(R.id.btn_dial);
        btn_reset = findViewById(R.id.btn_reset);

        initNumber();
        initDialNumPan();
        setListeners();
    }

    private void initDialNumPan() {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        adapter = new PhoneNumAdapter();
        rcv_num.setLayoutManager(manager);
        rcv_num.setAdapter(adapter);
        adapter.setDatas(dialNumList);
    }

    private void setListeners() {
        btn_dial.setOnClickListener(this);
        btn_reset.setOnClickListener(this);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                String dialNum = sb.toString().trim();
                if (dialNum.length() >= 16) {
                    return;
                }
                sb.append(data);
                if (sb.length() <= 16) {// 拨号的长度小于11
                    tv_num.setText(sb.toString());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == btn_dial) {
            String dialNum = tv_num.getText().toString().trim();
            if (TextUtils.isEmpty(dialNum)) {
                Toast.makeText(this, "请输入号码", Toast.LENGTH_SHORT).show();
            } else {
                for (Map.Entry<String, String> entry : PHONE_NUMBER.entrySet()) {
//                    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                    if (TextUtils.equals(dialNum, entry.getKey())) {// 拨叫的号码等于key,相当于短号,直接拨对应的长号
                        dialNum = entry.getValue();
                        break;
                    }
                }
                callPhone(dialNum);
            }
        }
        if (v == btn_reset) {
            tv_num.setText("");
            sb = new StringBuffer("");
        }
    }

    private class PhoneNumAdapter extends BaseRecyclerAdapter<String> {

        @Override
        public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
            View v = View.inflate(MyDialActivity.this, R.layout.item_phone_num, null);
            return new MyHoler(v);
        }

        @Override
        public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, String data) {
            MyHoler holer = (MyHoler) viewHolder;

            holer.tv_num.setText(data);
        }

        class MyHoler extends RecyclerView.ViewHolder {
            TextView tv_num;

            public MyHoler(@NonNull View itemView) {
                super(itemView);
                tv_num = itemView.findViewById(R.id.tv_num);
            }
        }
    }

    /**
     * 直接拨打电话
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    private void initNumber() {
        PHONE_NUMBER.put("551", "13900000001");
        PHONE_NUMBER.put("552", "13900000002");
        PHONE_NUMBER.put("553", "13900000003");
        PHONE_NUMBER.put("554", "13900000004");
        PHONE_NUMBER.put("555", "13900000005");
        PHONE_NUMBER.put("556", "13900000006");
        PHONE_NUMBER.put("557", "13900000007");
        PHONE_NUMBER.put("558", "13900000008");
        PHONE_NUMBER.put("559", "13900000009");

        dialNumList.add("1");
        dialNumList.add("2");
        dialNumList.add("3");
        dialNumList.add("4");
        dialNumList.add("5");
        dialNumList.add("6");
        dialNumList.add("7");
        dialNumList.add("8");
        dialNumList.add("9");
        dialNumList.add("*");
        dialNumList.add("0");
        dialNumList.add("#");
    }
}
